package com.jzit168.library_base.base

import android.app.Activity
import android.content.ActivityNotFoundException

/**
 *@Desc:
 *@Author: Android Developer
 *@Date: 2020/10/14
 *
 **/
object ActivityStackManager {

    private val activities = mutableListOf<Activity>()

    /**
     * 添加Activity到栈中
     */
    fun addActivity(activity: Activity) = activities.add(activity)

    /**
     * 移除存在栈中的Activity
     * @exception ActivityNotFoundException 不存在的Activity抛出此异常
     */
    fun removedActivity(activity: Activity) {
        if (activities.contains(activity)) {
            activities.remove(activity)
            activity.finish()
        } else {
            throw ActivityNotFoundException("${activity}不存在")
        }
    }

    /**
     * 清除所有在栈中的Activity
     */
    fun clearAll() = activities.filterNot { it.isFinishing }.onEach { it.finish() }
}